Wikipedia describes this ("All is for the best in the best of all possible worlds" as the "Leibnizian mantra" of Candide's mentor, Professor Pangloss.
The phrase "the best of all possible worlds" was coined by the German polymath Gottfried Leibniz in his 1710 work Essays of Theodicy on the Goodness of God, the Freedom of Man and the Origin of Evil. Leibniz's claim that the actual world we live in is the best of all possible worlds is the central argument in his theodicy – his attempt to resolve the problem of evil.
A central theme of Candide is that the title character is living a sheltered life in a kind of paradise, and being indoctrinated with Leibnizian optimism by Pangloss. The work describes the abrupt cessation of this lifestyle, followed by Candide's slow and painful disillusionment as he witnesses and experiences great hardships in the world.
© Haydn Thompson 2020